package com.dong.dynamicProgramming;

/**
 * @author dcz
 * @classname Demo5
 * @description 动态规划：不同路径障碍物
 * @date 2022/4/18 9:53
 */
public class Demo5 {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        //行
        int m = obstacleGrid.length;
        //列
        int n = obstacleGrid[0].length;
        //到达该位置的路线
        int[][] dp = new int[m][n];

        //初始化
        for (int i = 0; i < m; i++) {
            //遇到障碍物了，后续都到不了
            if (obstacleGrid[0][i] == 1){
                break;
            }
            dp[0][i] = 1;
        }

        for (int i = 0; i < n; i++) {
            if (obstacleGrid[i][0] == 1){
                break;
            }
            dp[i][0] = 1;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                //有障碍物时跳出本次循环
                if (obstacleGrid[i][j] == 1){
                    continue;
                }
                dp[i][j] = dp[i-1][j] + dp[j][i-1];
            }
        }
        return dp[n-1][m-1];

    }
}
